package ar.edu.unlam.tpfinal.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@Table(name="CLIENTS")
public class Client extends AbstractPersistentObject {

	private String firstName;
	
	private String lastName;
	
	private String telephone;
	
	private String email;
	
	private List<Address> addresses;
	
	private List<Transaction> transactions;
	
	public Client() {
		this(null, null, null, null, null, null);
	}
	
	public Client(String firstName, String lastName, Address address,
			      String telephone, String email, Transaction transaction) {
		super();
		this.setFirstName(firstName);
		this.setLastName(lastName);
		this.setAddresses(address);
		this.setTelephone(telephone);
		this.setEmail(email);
		this.setTransactions(transaction);
	}

	@Column(name = "NAME", columnDefinition="VARCHAR(25)", nullable = false)
	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@Column(name = "LAST_NAME", columnDefinition="VARCHAR(25)", nullable = false)
	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Column(name = "TELEPHONE", columnDefinition="VARCHAR(20)", nullable = false)
	public String getTelephone() {
		return this.telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	@Column(name = "EMAIL", columnDefinition="VARCHAR(60)", nullable = false)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	@OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE})
	@JoinTable(name = "CLIENT_ADDRESSES",
			   uniqueConstraints = { @UniqueConstraint(columnNames = {"CLIENT_OID", "ADDRESS_OID"})},
			   joinColumns = { @JoinColumn(name = "CLIENT_OID", unique = false) },
			   inverseJoinColumns = { @JoinColumn(name = "ADDRESS_OID", unique = false) } )
	@ForeignKey(name="FK_CLIENT_TO_ADDRESS")
	@LazyCollection(value = LazyCollectionOption.FALSE)
	public List<Address> getAddresses() {
		return addresses;
	}

	public void setAddresses(List<Address> addresses) {
		this.addresses = addresses;
	}
	
	public void setAddresses(Address address) {
		if(this.addresses == null){
			this.addresses = new ArrayList<Address>();
		}
		this.addresses.add(address);
	}

	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "clients")
	@ForeignKey(name="FK_CLIENT_TO_TX")
	public List<Transaction> getTransactions() {
		return transactions;
	}

	public void setTransactions(List<Transaction> transactions) {
		this.transactions = transactions;
	}
	
	public void setTransactions(Transaction transaction) {
		if(this.transactions == null){
			this.transactions = new ArrayList<Transaction>();
		}
		this.transactions.add(transaction);
	}
	
}
